var basePath = $("#basePath").val();
var userType = $("#userType").val();
$(document).ready(function (){
    menuCss("保养");
    queryUpkeepInfo();
});


function queryUpkeepInfo() {
    var upkeepId = $("#upkeepId").val();
    var url = basePath+"/upkeep/queryUpkeepInfo";
    publicAjax(url,"POST",{upkeepId:upkeepId},function (data) {
        if(data.ret == 200){
            bindUpkeepInfoData(data.data);
        } else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}

var partMap = new Map();
function bindUpkeepInfoData(obj) {

    $("#deviceNameSpan").html(obj.deviceName == null ? '' : obj.deviceName );
    $("#deviceModelSpan").html(obj.deviceModel == null ? '' : obj.deviceModel );
    $("#snSpan").html(obj.sn == null ? '' : obj.sn );
    if(userType == 1101){

    }else{
        $("#hospitalNameSpan").html(obj.hospitalName == null ? '' : obj.hospitalName );
    }

    if(obj.examine == 1){
        $("#examineSpan").html('通过');
    }else if(obj.examine == 0){
        $("#examineSpan").html('不通过');
    }else{
        $(".examine").hide();
    }
    $("#examineCommentSpan").html(obj.examineComment == null ||  obj.examineComment == '' ? '无' : obj.examineComment);

    $("#serviceNameSpan").html(obj.serviceName == null ? '' : obj.serviceName);
    $("#serviceContactsSpan").html(obj.serviceContacts == null ? '' : obj.serviceContacts);
    $("#servicePhoneSpan").html(obj.servicePhone == null ? '' : obj.servicePhone);
    switch (obj.serviceType){
        case 1102 :
            $("#serviceTypeNameSpan").html("厂商");
            break;
        case 1103:
            $("#serviceTypeNameSpan").html("维修商");
            break;
        case 1105 :
            $("#serviceTypeNameSpan").html("经销商");
            break;
        default:
            $(".service").hide();
            $(".service1").html("<td class='text-center' colspan='6'><label class='text-muted'>医院没有填写服务商信息</label></td>")
    }
    $("#createTimeSpan").html(obj.createTime == null ? '' : obj.createTime);
    $("#engineerExpectedTimeSpan").html(obj.engineerExpectedTime == null ? '' : obj.engineerExpectedTime);
    $("#upkeepDateSpan").html(obj.upkeepDate == null ? '' : obj.upkeepDate );
    $("#applicantNameSpan").html(obj.upkeepApplicant == null ? '' : obj.upkeepApplicant );
    $("#upkeepApplicantPhoneSpan").html(obj.upkeepApplicantPhone == null ? '' : obj.upkeepApplicantPhone );
    $("#deviceStatusStrSpan").html(obj.startDeviceStatusStr == null ? '' : obj.startDeviceStatusStr );
    $("#handleTimeSpan").html(obj.handleTime == null ? '' : obj.handleTime );
    $("#dispathTimeSpan").html(obj.dispathTime == null ? '' : obj.dispathTime );
    $("#hospitalContactsSpan").html(obj.hospitalContacts == null ? '' : obj.hospitalContacts );
    $("#hospitalPhoneSpan").html(obj.hospitalPhone == null ? '' : obj.hospitalPhone );
    $("#maintainerNameSpan").html(obj.maintainerName == null ? '' : obj.maintainerName );
    $("#engineerNameSpan").html(obj.engineerName == null ? '' : obj.engineerName );
    $("#mobileSpan").html(obj.mobile == null ? '' : obj.mobile );
    $("#travelTimeSpan").html((obj.travelTime == null ? '' : (obj.travelTime/10)+"小时") );
    $("#upkeepEndTimeSpan").html(obj.upkeepEndTime == null ? '' : obj.upkeepEndTime );
    $("#upkeepContentSpan").html(obj.upkeepContent == null ? '' : obj.upkeepContent );
    $("#confirmTimeSpan").html(obj.confirmTime == null ? '' : obj.confirmTime );
    $("#confirmPersonNameSpan").html(obj.confirmPersonName == null ? '' : obj.confirmPersonName );
    $("#commentSpan").html(obj.comment == null ? '' : obj.comment );
    $("#statusStrSpan").html(obj.statusStr == null ? '' : obj.statusStr );
    $("#remarkSpan").html(obj.remark == null ? '' : obj.remark );
    $("#terraceHandlePersonNameSpan").html(obj.terraceHandlePersonName==null?'':obj.terraceHandlePersonName);
    $("#endDeviceStatusStrSpan").html(obj.endDevicStatusStr==null?'':obj.endDevicStatusStr);

    if(obj.appointView == null){
        $(".current").hide();
    }else{
        $("#currentServiceTypeSpan").html(obj.appointView.instructedTypeName == null ? '' : obj.appointView.instructedTypeName);
        $("#currentServiceNameSpan").html(obj.appointView.instructedName == null ? '' : obj.appointView.instructedName);
        $("#currentServiceContactsSpan").html(obj.appointView.engineerName == null ? '' : obj.appointView.engineerName);
        $("#currentServicePhoneSpan").html(obj.appointView.engineerPhone == null ? '' : obj.appointView.engineerPhone);
    }


    //绑定事件
    $("#timeAxis").html("");
    var str = "";
    if(obj.eventList == null || obj.eventList.length == 0){
        str = "暂无事件";
        $("#timeline").html(str);
    }else{
        $.each(obj.eventList,function (index,item) {
            var classs="";
            var color="";
            switch (item.type){
                case 1700 :classs="fa fa-wrench";
                    color = "#f9b229";
                    break;
                case 1701 :classs="fa fa-cogs";
                    color = "#82ca2b";
                    break;
                case 1702 :classs="fa fa-rocket";
                    color = "#10458f";
                    break;
                default : classs="fa fa-clock-o";
                    color="#5082eb";
            }

            str+="<li><i class='"+classs+"' style='color: #fff;background:"+color+"'></i>"
                +"<div class='timeline-item'>"
                +"<h3 class='timeline-header' style='background: #fff'>"
                +"<span style='color: #000'>"+(item.eventDateTime==null?'':item.eventDateTime)+"</span>"
                +"</h3>"
                +"<div class='timeline-body'>"
                +"<span style='color: #5082eb'>"+(item.eventTitle==null?'': '【'+item.eventTitle+'】 ')+"</span>"
                +item.eventContent
                +"</div>"
                +"<div>";

            if(item.fileList != null && item.fileList.length > 0){
                $.each(item.fileList,function (index,items) {
                    str+= "<a href='"+items.fileUrl+"'>"+items.fileName+"</a><br/>";
                })
            }
            str+="</div>"
                +"</div>"
                +"</li>";
        });
        str+="<li><i class='fa fa-clock-o bg-gray'></i></li>";
        $("#timeAxis").html(str);

        if($(".imgList").length > 0){
            $(".imgList").viewer("destroy");
            $(".imgList").viewer();
        }
    }


    //绑定文件信息
    $("#upkeep_file_list").html("");
    var strs = "";
    if(obj.fileList == null || obj.fileList.length == 0){
        strs = "<tr><td colspan='10' class='text-muted text-center'>暂无文件</td></tr>";
    }else{
        $.each(obj.fileList,function (index,item) {
            var fileName = item.fileName + "." + item.fileSuffix;
            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                "<td class='text-muted text-center'>"+(item.fileTypeName == null?'保养工单':item.fileTypeName)+"</td>" +
                "<td class='text-muted text-center'><a href='javascript:showFile(\""+item.fileUrl+"\",\""+fileName+"\")'>"+(item.fileName == null?'':item.fileName)+"</a></td>" +
                "<td class='text-muted text-center'><button type='button' class='btn btn-danger btn-xs' title='下载' onclick='fileDownload(\""+item.fileName+"\",\""+item.fileUrl+"\",\""+item.fileSuffix+"\")'><i class='fa fa-fw fa-download'></i></button></td>" +
                "</tr>";
        });
    }
    $("#upkeep_file_list").html(strs);
    //绑定备件
    $("#upkeep_part_List").html("");
    var str1 = "";
    if(obj.partList == null || obj.partList.length == 0){
        str1 = "<tr><td colspan='17' class='text-muted text-center'>暂无备件信息</td></tr>"
    }else{
        $.each(obj.partList,function (index,item) {
            var oldFileLength = item.oldFileList == null ? 0 : item.oldFileList.length;
            var newFileLength = item.newFileList == null ? 0 : item.newFileList.length;
            str1 += "<tr>" +
                "<td class='text-muted text-center'>"+(item.partExplainName == null?'':item.partExplainName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.newPartName == null?'':item.newPartName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.newPartBrand == null?'':item.newPartBrand)+"</td>" +
                "<td class='text-muted text-center'>"+(item.newPartModel == null?'':item.newPartModel)+"</td>" +
                "<td class='text-muted text-center'>"+(item.newPartSn == null?'':item.newPartSn)+"</td>" +
                "<td class='text-muted text-center'>"+(item.newPartPrice == null?'':item.newPartPrice/100)+"</td>" +
                "<td class='text-muted text-center'>"+(item.oldPartName == null?'':item.oldPartName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.oldPartBrand == null?'':item.oldPartBrand)+"</td>" +
                "<td class='text-muted text-center'>"+(item.oldPartModel == null?'':item.oldPartModel)+"</td>" +
                "<td class='text-muted text-center'>"+(item.oldPartSn == null?'':item.oldPartSn)+"</td>" +
                "<td class='text-muted text-center'>"+(item.oldPartPrice == null?'':item.oldPartPrice/100)+"</td>" +
                "<td class='text-muted text-center'><a href='javascript:showPartFile("+item.partId+",1)'>"+oldFileLength+"</a></td>" +
                "<td class='text-muted text-center'><a href='javascript:showPartFile("+item.partId+",2)'>"+newFileLength+"</a></td>" +
                "</tr>";
            partMap.put(item.partId,item);
        });
    }
    $("#upkeep_part_List").html(str1);

    //绑定评分
    $("#scoreTable").html('<tr><td class="text-center" colspan="2">暂无评分信息</td></tr>');
    if(obj.commentView && obj.commentView.commentInfoList.length){
        $("#scoreTable").html("");
        $.each(obj.commentView.commentInfoList,function (index,item) {
            $("#scoreTable").append("<tr>\
                <td>"+item.dividTypeName+"</td>\
                <td>"+item.value+"分（"+scoreText(item.value)+"）</td>\
            </tr>");
        })
        $("#scoreTable").append("<tr>\
            <td>评论</td>\
            <td>"+obj.commentView.commentContent+"</td>\
        </tr>");
    }


    //绑定指派信息
    $("#assign_upkeep_list").html('<tr><td class="text-center" colspan="6">暂无指派信息</td></tr>');
    if(obj.appointList && obj.appointList.length > 0){
        var html = "";
        $.each(obj.appointList,function (index,item){
            html+="<tr>" +
                "<td class='text-muted text-center'>"+(item.instructName == null?'':item.instructName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.instructTypeName == null?'':item.instructTypeName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.instructedName == null?'':item.instructedName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.instructedTypeName == null?'':item.instructedTypeName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.createTime == null?'':item.createTime)+"</td>" +
                "<td class='text-muted text-center'>"+(item.engineerName == null?'':item.engineerName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.engineerPhone == null?'':item.engineerPhone)+"</td>" +
                "</tr>";

        });
        $("#assign_upkeep_list").html(html);
    }


}

$("#showFileDiv").iziModal({
    title: "备件图片",
    headerColor: "#00c0ef",
    overlayClose:true,
    autoOpen:false,
    history:false,
    zindex: 9999,
    width:800,
    focusInput: false,
    bodyOverflow: true,
    fullscreen: true,
    openFullscreen: true,
    closeOnEscape: true,
    overlay: true,
    overlayClose: false,
    onOpening: function () {},
    onOpened: function () {},
    onClosing: function () {},
    onClosed: function () {}
});

function showPartFile(id,type){
    $("#oldFileImgDiv").html("");
    $("#newFileImgDiv").html("");
    var fileList;
    if(type == 1){
        fileList = partMap.get(id).oldFileList;
    }else{
        fileList = partMap.get(id).newFileList;
    }

    if(fileList == null){
        return;
    }else{
        $.each(fileList,function (index,item){
            if(type == 1){
                $("#oldFileImgDiv").append("<img style='width:45%' height='400px;' src='"+item.fileUrl+"'/>");
                $("#newFileDiv").hide();
                $("#oldFileDiv").show();
            }else{
                $("#newFileImgDiv").append("<img style='width:45%' height='400px;' src='"+item.fileUrl+"'/>");
                $("#oldFileDiv").hide();
                $("#newFileDiv").show();
            }
        });
    }

    $("#showFileDiv").find("img").viewer("destroy");
    $("#showFileDiv").find("img").viewer();

    $("#showFileDiv").iziModal("open",this);
}

function showFile(url,file){
    $("#file_show_modal").iziModal({
        zindex:999999,
        headerColor: '#00c0ef',
        title: '预览',
        subtitle: file,
        overlayClose: true,
        history:false,
        iframe : true,
        iframeURL:url,
        fullscreen: true,
        openFullscreen: false,
        onClosed:function(){
            $('#file_show_modal').iziModal('destroy');
        }
    });
    $("#file_show_modal").iziModal('open');
}

function fileDownload(fileName,filePath,fileSuffix){
    location.href= basePath+"/file/fileDownload?filePath="+filePath+"&fileName="+fileName;
}